<?php
namespace app\index\controller;
use think\Controller;
use think\Db;

use app\lib\aliyun\Api as aliyunApi;
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;

class Base extends Controller
{
    public $exception = [];

    public function __construct(){
        parent::__construct();

        $login_user = session("cch_user");
        if (empty($login_user)) {
            $cch_user_id = cookie('cch_user_id');
            if ( $cch_user_id > 0 ) {
                $login_user = Db::name('user')->where('id',$cch_user_id)->find();
                if (!empty($login_user)) {
                    session('cch_user',$login_user);
                }
            }
        }
        $action = strtolower(request()->action());
        $exception = array_map('strtolower',$this->exception);
        if(empty($login_user) && !in_array($action, $exception)){
            session('login_redirect_url',$_SERVER['REQUEST_URI']);
            $this->redirect(url('user/login'));
        }
        $this->login_user = $login_user;
        $this->page_info = [
            'action'=>$action,
            'controller'=>strtolower(request()->controller()),
            'path'=>request()->path()
        ];
        $this->page_info['controller'] = explode('/', $this->page_info['path'])[1];

        $configs = Db::name('config')->field('key,value')->select();
        $configs = arrayParamExtract($configs,'key','value');

        $this->assign([
            'login_user'=>$login_user,
            'configs'=>$configs,
        ]);
    }


    

    /**
     * 上传图片
     * @Author   CCH
     * @DateTime 2020-08-15T17:26:24+0800
     * @return   [type]                   [description]
     */
    public function upload(){
        $file = $_FILES['file'];
        if (empty($file)) {
            return json(['code'=>5000,'msg'=>'未检测到图片']);
        }
        $ext = explode(".", $file['name']);//拆分获取图片名
        $ext = $ext[count($ext) - 1];//取图片的后缀名
        $ext = strtolower($ext);
        $types = ["jpg", "gif", 'png', 'bmp','jpeg'];
        if (!in_array($ext, $types)) {
            return json(['code'=>5000,'msg'=>'文件格式不支持，请上传标准图片文件']);
        }
        $result = uploadFile($file['name'],$file['tmp_name'],date('Ymd').'/');

        if ($result['status'] == 1) {
            $result = [
                'code'=>2000,'msg'=>'上传成功：'.$file['name'],
                'extension'=>$ext,
                'savename'=>$result['data'],
                'filename'=>$file['name']
            ];
            return json($result);
        }else{
            return json(['code'=>5000,'msg'=>$result['msg']]);
        }
    }


}
